#include "DoubleList.h"
#include <stdio.h>
//calloc
#include <stdlib.h>
#include <string.h>

//create a new DoubleList -->create a headnode
DNodep DoubleList_Create(void)
{
	DNodep nodep = NULL;
	nodep =(DNode *)calloc(1, sizeof(DNode));
	
	//Initiaize the Struct 
	//strcpy(nodep->data, str);
	nodep->prev = nodep;
	nodep->next = nodep;

	return nodep;
}


DNodep DoubleNode_Create(char *str)
{
	DNodep nodep = NULL;
	nodep =(DNode *)calloc(1, sizeof(DNode));
	
	//Initiaize the Struct 
	strcpy(nodep->data, str);
	nodep->prev = nodep;
	nodep->next = nodep;

	return nodep;
}


int DoubleList_TailInsert(DNodep headp, DNodep nodep)
{
		if((headp == NULL)||(nodep == NULL))
		{
			perror("the input DNodep is illegal!");
			return -1;
		}
		//find the tail's point, renew its next
		headp->prev->next = nodep;
		nodep->prev = headp->prev; //9-5 修改错误句：nodep->prev = headp->next; 
		nodep->next = headp;
		headp->prev = nodep;

		return 0;
}


DNodep DoubleList_Delete(DNodep headp, DNodep nodep)
{
		DNodep tempp = NULL;

		if((headp == NULL)||(nodep == NULL))
		{
			perror("the input DNodep is illegal!");
			return NULL;
		}

		if(headp == nodep)//delete the head
		{
			tempp = headp->next;
			headp->prev->next = tempp;
			tempp->prev = headp->prev;
			
			free(headp);
			headp = NULL;
			return tempp; //new head
		}

		nodep->prev->next = nodep->next;
		nodep->next->prev = nodep->prev;
		
		free(nodep);
		nodep = NULL;
		return headp;
}


